// pages/addressInfo/addressInfo.js
var app = getApp();
const tool = require('../../config/tool.js');
import { addAddress, lookAddress, editAddress } from '../../config/https';
import Notify from '../../vant/notify/notify';
import Dialog from '../../vant/dialog/dialog';
Page({

  /**
   * 页面的初始数据
   */
  data: {
    username: null, //收货人姓名
    mobile: null, //收货人手机号
    address: null, //收货人地址
    detail: null, //详细信息
    checked: false,
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    if (options.info){
      this.setData(JSON.parse(options.info))
    }
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  usernameInputEvent(e){
    this.data.username = e.detail.value
  },

  mobileInputEvent(e) {
    this.data.mobile = e.detail.value
  },

  detailInputEvent(e) {
    this.data.detail = e.detail.value
  },

  formSubmit(e){
    const data = e.detail.value;
    const address = this.data.address;
    if (!tool.u_Reg(data.username, 'notEmpty')) {
      this.failNot("请填写收货人姓名!")
      return
    }
    if (!tool.u_Reg(data.mobile, 'notEmpty')) {
      this.failNot("请填写收货人手机号!")
      return
    }
    if (!tool.u_Reg(data.mobile, 'phone')) {
      this.failNot("手机号码有误，请重新填写")
      return
    }
    if (!tool.u_Reg(address, 'notEmpty')) {
      this.failNot("请选择收货地址!")
      return
    }
    let params = {
      user_id: app.globalData.user_id,
      shperson: data.username,
      mobile: data.mobile,
      region: address,
      detail_info: data.detail,
      status: data.default ? 1 : 0,
      longitude: this.data.longitude,
      latitude: this.data.latitude,
    }
    if (this.options.info){
      params.address_id = this.data.address_id
      editAddress(params).then(res => {
        this.succNot('编辑成功')
        setTimeout(() => { wx.navigateBack() }, 1000)
      })
    }else{
      addAddress(params).then(res => {
        this.succNot('添加成功')
        if (this.options.submit){
          //获取页面栈
          let pages = getCurrentPages();
          //获取上一个页面
          let prev = pages[pages.length - 2];
          //调用上一个页面的setData方法，从而达到naviagtreBack返回传参的效果
          prev.setData({ address: res.address });
        }
        setTimeout(() => { wx.navigateBack() }, 1000)
      })
    }
    
  },
  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  //选择地址
  chooseLocation() {
    wx.chooseLocation({
      success: res => {
        this.setData(res)
      },
      fail: res => {
        if (res.errMsg == "chooseLocation:fail auth deny"){
          Dialog.confirm({
            confirmButtonOpenType: "openSetting",
            message: '获取微信定位失败，请授权小程序获取位置信息'
          }).then(res => {
            // console.log(res)
          }).catch(err => {
            // console.log(err)
          });
        }
      }
    })
  },

  // 在打开授权设置页后回调
  bindopensetTap(e) {
    e.detail.authSetting['scope.userLocation'] && (app.globalData.is_open = 1)
  },

  succNot(msg) {
    Notify({
      text: msg,
      backgroundColor: '#1989fa'
    });
  },

  failNot(msg) {
    Notify({
      text: msg,
      backgroundColor: '#f44'
    });
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})